package com.ruoyi.ai.api;

import com.ruoyi.ai.api.bo.WxMaAddBo;
import com.ruoyi.ai.api.bo.WxMaConsumeBo;
import com.ruoyi.ai.domain.bo.WxNumChangeBo;
import com.ruoyi.ai.enums.ChangeTypeEnum;
import com.ruoyi.ai.service.IWxNumChangeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.ip.AddressUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 微信小程序接口-数量变更
 **/
@RestController
@AllArgsConstructor
@Slf4j
@RequestMapping("/wx/ma/numChange/")
public class WxMaNumChangeController extends BaseController {

    private final IWxNumChangeService wxNumChangeService;

    /**
     * 新增消耗记录
     */
    @PostMapping("/consume")
    public R<Void> consume(@Validated @RequestBody WxMaConsumeBo wxMaConsumeBo) {
        wxMaConsumeBo.setUserType(UserType.WX_CUSTOMER.getUserType());
        wxNumChangeService.consume(wxMaConsumeBo);
        return R.ok();
    }

    /**
     * 广告奖励增加次数
     */
    @PostMapping("/addChatNums")
    public R<Void> addChatNums(@Validated @RequestBody WxMaAddBo wxMaAddBo, HttpServletRequest request) {
        // 获取用户IP地址
        String ip = ServletUtils.getClientIP();
        wxMaAddBo.setUserIp(ip);

        // 获取设备信息（从请求头获取）
        String userAgent = request.getHeader("User-Agent");
        wxMaAddBo.setDeviceInfo(userAgent);

        log.info("用户增加次数，IP: {}, 设备: {}", ip, userAgent);

        wxNumChangeService.addNum(wxMaAddBo);
        return R.ok();
    }
}
